package com.xiangban.yanglaodajie_platform.service;


import com.xiangban.yanglaodajie_platform.dao.DeliveryAddressMapper;
import com.xiangban.yanglaodajie_platform.entity.DeliveryAddress;
import com.xiangban.yanglaodajie_platform.entity.dto.DeliveryAddressDto;
import com.xiangban.yanglaodajie_platform.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/4/9 9:49
 */
@Service
public class DeliveryAddressService {

    @Autowired
    private DeliveryAddressMapper deliveryAddressMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private CommonUtils commonUtils;

    public List<DeliveryAddressDto> getList(DeliveryAddress deliveryAddress){
        deliveryAddress.setUserId(Integer.parseInt(commonUtils.getUserId(request)));
        List<DeliveryAddressDto> address = deliveryAddressMapper.findAddress(deliveryAddress);
        return address;
    }

    public int address(DeliveryAddress deliveryAddress){
        deliveryAddress.setUserId(Integer.parseInt(commonUtils.getUserId(request)));
        deliveryAddress.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
       return deliveryAddressMapper.saveAddress(deliveryAddress);
    }

    public int deleteAddress(DeliveryAddress deliveryAddress) {
        deliveryAddress.setUserId(Integer.parseInt(commonUtils.getUserId(request)));
       return deliveryAddressMapper.deleteAddress(deliveryAddress);
    }

    public int updateAddress(DeliveryAddress deliveryAddress) {
        deliveryAddress.setUserId(Integer.parseInt(commonUtils.getUserId(request)));
        if (deliveryAddress.getIsDefault() ==1){
            deliveryAddressMapper.reset(deliveryAddress);
        }
        deliveryAddress.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return deliveryAddressMapper.updateById(deliveryAddress);
    }

    public DeliveryAddressDto findObject(DeliveryAddress deliveryAddress){
        deliveryAddress.setUserId(Integer.parseInt(commonUtils.getUserId(request)));
        return deliveryAddressMapper.findObject(deliveryAddress);
    }
}
